#include "Bldc.h"
static void Bldc_UV(uint16_t CCR);
static void Bldc_WV(uint16_t CCR);
static void Bldc_WU(uint16_t CCR);
static void Bldc_VU(uint16_t CCR);
static void Bldc_VW(uint16_t CCR);
static void Bldc_UW(uint16_t CCR);

BldcRunState_t xBldcRunMode;//bldc运行状态
ForceRunState_t xForceRunState;//强拖状态
SensorlessInfo_t xSensorlessInfo;
void BldcPwmControl(BldcPhase_t ePhase)
{
    uint16_t CCR = xMotorMode.CCR;
    switch(ePhase)
    {
        case PHASE1:
            Bldc_UV(CCR);
            break;
        case PHASE2:
            Bldc_UW(CCR);
            break;
        case PHASE3:
            Bldc_VW(CCR);
            break;            
        case PHASE4:
            Bldc_VU(CCR);
            break;            
        case PHASE5:
            Bldc_WU(CCR);
            break;
        case PHASE6:
            Bldc_WV(CCR);
            break;
        default:
            PWM_OFF();
            break;
    }
}

void BldcOff(void)
{
    PWM_OFF();
}

static void Bldc_UV(uint16_t CCR)
{
    PWM_CH1H_Ctrl(PWM_ON,CCR);//CH1上管PWM，下管利用互补机制取反
    PWM_CH2N_ON();//CH2下管高电平，上管强制低电平
    PWM_CH3_OFF(); 
}

static void Bldc_WV(uint16_t CCR)
{
    PWM_CH3H_Ctrl(PWM_ON,CCR);//CH3上管PWM，下管利用互补机制取反
    PWM_CH2N_ON();//CH2下管高电平，上管强制低电平
    PWM_CH1_OFF();   
}

static void Bldc_WU(uint16_t CCR)
{
    PWM_CH3H_Ctrl(PWM_ON,CCR);//CH3上管PWM，下管利用互补机制取反
    PWM_CH1N_ON();//CH1下管高电平，上管强制低电平
    PWM_CH2_OFF();
}

static void Bldc_VU(uint16_t CCR)
{ 
    PWM_CH2H_Ctrl(PWM_ON,CCR);//CH2上管PWM，下管利用互补机制取反
    PWM_CH1N_ON();//CH1下管高电平，上管强制低电平
    PWM_CH3_OFF(); 
}

static void Bldc_VW(uint16_t CCR)
{  
    PWM_CH2H_Ctrl(PWM_ON,CCR);//CH2上管PWM，下管利用互补机制取反
    PWM_CH3N_ON();//CH3下管高电平，上管强制低电平
    PWM_CH1_OFF();     
}

static void Bldc_UW(uint16_t CCR)
{
    PWM_CH1H_Ctrl(PWM_ON,CCR);//CH1上管PWM，下管利用互补机制取反
    PWM_CH3N_ON();//CH3下管高电平，上管强制低电平
    PWM_CH2_OFF();      
}
